package com.itheima.xiancheng.homework.homework6;

import java.util.Random;

public class Put implements Runnable {
    private Cangku ck;
    private Random r = new Random();

    public Put(Cangku ck) {
        this.ck = ck;
    }

    @Override
    public void run() {

        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (ck) {
                if (!ck.isFlag()) {
                    int i = r.nextInt(10) + 1;
                    while ((i + ck.getThCount()) <=10) {
                        ck.put();
                        System.out.println("生产存入1个单位货物，现在仓库剩余" + ck.getThCount());
                    }
                    ck.setFlag(true);
                    ck.notify();
                } else {
                    try {
                        ck.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }

        }
    }
}
